package com.ying.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.CompositeCodec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Value("${spring.redis.password:}")
    private String redisPassword;

    @Value("${redisson.connection-pool-size:32}")
    private int connectionPoolSize;

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        Config config = new Config();

        // 配置单节点连接
        config.useSingleServer()
                .setAddress("redis://" + redisHost + ":" + redisPort)
                .setConnectTimeout(3000)     // 连接超时3秒
                .setTimeout(3000)            // 命令执行超时3秒
                .setRetryAttempts(3)         // 失败重试3次
                .setRetryInterval(1500);     // 重试间隔1.5秒

        Codec value = new JsonJacksonCodec(); // 值使用 JSON 序列化
        Codec key = new StringCodec();         // 键使用字符串序列化
        CompositeCodec codec = new CompositeCodec(key, value); // 组合 Codec
        JsonJacksonCodec jsonJacksonCodec = new JsonJacksonCodec();
        config.setCodec(codec);
        return org.redisson.Redisson.create(config);

    }
}